/*
 * @author : TF-BJ-C064
 * @creation : 2014-7-24 下午12:27:07
 * @description : 
 *
 */

package com.dao;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;

import com.action.park.MyPayStatisticsAction;
import com.base.BaseDao;
import com.bean.ParkAccountRecord;
import com.bean.PayMent;
import com.bean.TfAccountRecord;
import com.tool.DateTimeTools;
import com.util.GUtil;

public class PayStatisticsDao extends BaseDao {

	private Logger log = Logger.getLogger(PayStatisticsDao.class);
	private static String Tag = PayStatisticsDao.class.getName();


	/**
	 * 按天收支统计
	 * <p>Title: queryByDay</p>
	 * <p>Description: </p>
	 * @param payInOut 收入和支出列表
	 * @param payStat  根据category类型统计的列表
	 * @param daytime  日期，精确到天,e.g: 2014-5-06
	 * @return
	 * @throws Exception
	 * @see com.service.PayStatisticsService#queryByDay(com.bean.PayMent, com.bean.PayMent, java.sql.Timestamp)
	 */
	public String queryByDay(List<ParkAccountRecord> payInOut, List<ParkAccountRecord> payStat, String daytime, Long userId)throws Exception {
		return this.queryByTime(payInOut, payStat, daytime, userId);
	}
	/**
	 * 按照传入的日期统计收支、按类别的收支
	 * <p>Title: queryByTime</p>
	 * <p>Description:
	 * 		1. 传入daytime精确到天，统计某天的收支和按类别的收支
	 * 		2. 传入daytime精确到月，统计某月的收支和按类别的收支
	 * 		1. 传入daytime精确到年，统计某年的收支和按类别的收支
	 *  </p>
	 * @param payInOut
	 * @param payStat
	 * @param daytime
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public String queryByTime(List<ParkAccountRecord> payInOut, List<ParkAccountRecord> payStat, String daytime, Long userId)throws Exception {

		if(payInOut==null || payStat==null || daytime==null || userId==null)
			return "传入参数均不能为空"; //GUtil.FAILE;
	
		session = this.getSession();
		String hql = "select new com.bean.ParkAccountRecord(sum(amount), type) from ParkAccountRecord p where parkManager.userId=? " +
				" and createTime like '"+daytime+"%'" +
				" GROUP BY type";
		Query query = session.createQuery(hql);
		query.setLong(0, userId);
		List<ParkAccountRecord> p1 = query.list();
		payInOut.addAll(p1);

		hql = "select new com.bean.ParkAccountRecord(sum(amount),type,category) from ParkAccountRecord p where parkManager.userId=? " +
				" and createTime like '"+daytime+"%'" +
				" GROUP BY category";
		query = session.createQuery(hql);
		query.setLong(0, userId);
		List<ParkAccountRecord> p2 = query.list();
		payStat.addAll(p2);

		return GUtil.SUCCESS;
	}
	

	/**
	 * 按周收支统计
	 * <p>Title: queryByMonth</p>
	 * <p>Description: 
	 * 		1. 统计本周的收入、支出  写入payInOut
	 * 		2. 统计本周按类别的费用金额，写入payStat
	 * 		3. 统计本周按天来计算的收入、支出map列表，写入paylistInOutByDay
	 * </p>
	 * @param payInOut  收入和支出列表
	 * @param payStat   根据category类型统计的列表
	 * @param paylistInOutByDay  按天排列的收入和支持列表
	 * @param daytime  日期，精确到日, e.g: 2014-05-06
	 * @param userId
	 * @return
	 * @throws Exception
	 * @see com.service.PayStatisticsService#queryByMonth(java.util.List, java.util.List, java.util.List, java.lang.String, java.lang.Long)
	 */
	public Double [][] queryByWeek(List<ParkAccountRecord> payInOut,
			List<ParkAccountRecord> payStat,
			Map<Double, Double> paylistInOutByDay, String weektime, Long userId)
					throws Exception {

		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		//String weektime = "2014-05-06";
		Date timelong = formatDate.parse(weektime);
		Timestamp inTime = new Timestamp(timelong.getTime());
		Timestamp outStartTime = new Timestamp(timelong.getTime());
		Timestamp outEndTime = new Timestamp(timelong.getTime());
		DateTimeTools.getWeekDays(inTime, outStartTime, outEndTime);//获取月的开始和结束时间，从而获取本月的最后一天，即获取本月的长度

		//查询 payInOut、payStat
		session = this.getSession();
		String hql = "select new com.bean.ParkAccountRecord(sum(amount), type) from ParkAccountRecord p where parkManager.userId=? " +
				" and createTime between ? and ? " +
				" GROUP BY type";
		Query query = session.createQuery(hql);
		query.setLong(0, userId);
		query.setTimestamp(1, outStartTime);
		query.setTimestamp(2, outEndTime);
		List<ParkAccountRecord> p1 = query.list();
		payInOut.addAll(p1);

		hql = "select new com.bean.ParkAccountRecord(sum(amount),type,category) from ParkAccountRecord p where parkManager.userId=? " +
				" and createTime between ? and ? " +
				" GROUP BY category";
		query = session.createQuery(hql);
		query.setLong(0, userId);
		query.setTimestamp(1, outStartTime);
		query.setTimestamp(2, outEndTime);
		List<ParkAccountRecord> p2 = query.list();
		payStat.addAll(p2);

		//统计7天数据
		//query paylistInOutByDay

		int enddate = 7;// outEndTime.getDate();
		Double[][] data = new Double[3][enddate];
		for(int i=0; i<3; i++){
			for(int j=0; j<enddate; j++){
				if(i==0)
					data[i][j] = (double)(j+1);
				else
					data[i][j] = 0.0;
			}
		}

		hql = "SELECT new com.bean.ParkAccountRecord( year(createTime),month(createTime),day(createTime),sum(amount),type ) " +
				"from ParkAccountRecord "+
				"where parkManager.userId=? and createTime between ? and ?  "+
				"GROUP BY day(createTime),type ";
		query = session.createQuery(hql);
		query.setLong(0, userId);
		query.setTimestamp(1, outStartTime);
		query.setTimestamp(2, outEndTime);
		List<ParkAccountRecord> list = query.list();
		if(list!=null){
			for(ParkAccountRecord par : list){
				if(par!=null)
					data[par.getType()][par.getDay() - outStartTime.getDate()] = Math.abs(par.getAmount());
			}
		}else{
			return null;
		}
		log.info( this.getClass()+".queryByWeek() "+data.toString());
		return data;
		
	}


	/**
	 * 按月收支统计
	 * <p>Title: queryByMonth</p>
	 * <p>Description: 
	 * 		1. 统计本月的收入、支出  写入payInOut
	 * 		2. 统计本月按类别的费用金额，写入payStat
	 * 		3. 统计本月按天来计算的收入、支出map列表，写入paylistInOutByDay
	 * </p>
	 * @param payInOut  收入和支出列表
	 * @param payStat   根据category类型统计的列表
	 * @param paylistInOutByDay  按天排列的收入和支持列表
	 * @param daytime  日期，精确到月, e.g: 2014-05
	 * @param userId
	 * @return
	 * @throws Exception
	 * @see com.service.PayStatisticsService#queryByMonth(java.util.List, java.util.List, java.util.List, java.lang.String, java.lang.Long)
	 */
	public Double [][] queryByMonth(List<ParkAccountRecord> payInOut,
			List<ParkAccountRecord> payStat,
			Map<Double, Double> paylistInOutByDay, String monthtime, Long userId)
					throws Exception {
		
		String ret = this.queryByTime(payInOut, payStat, monthtime, userId);//查询 payInOut、payStat
		
		if( ret.equals(GUtil.SUCCESS) ){

			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM");
			//String datetime = "2014-05";
			Date timelong = formatDate.parse(monthtime);
			Timestamp inTime = new Timestamp(timelong.getTime());
			Timestamp outStartTime = new Timestamp(timelong.getTime());
			Timestamp outEndTime = new Timestamp(timelong.getTime());
			DateTimeTools.getMonthDays(inTime, outStartTime, outEndTime);//获取月的开始和结束时间，从而获取本月的最后一天，即获取本月的长度

			int enddate = outEndTime.getDate();
			Double[][] data = new Double[3][enddate];
			for(int i=0; i<3; i++){
				for(int j=0; j<enddate; j++){
					if(i==0)
						data[i][j] = (double)(j+1);
					else
						data[i][j] = 0.0;
				}
			}

			session = this.getSession();
			String hql = "SELECT new com.bean.ParkAccountRecord( year(createTime),MONTH(createTime),day(createTime),sum(amount),type ) " +
					"from ParkAccountRecord "+
					"where parkManager.userId=? and createTime like '"+monthtime+"%'"+
					"GROUP BY year(createTime),MONTH(createTime),day(createTime),type ";
			Query query = session.createQuery(hql);
			query.setLong(0, userId);
			List<ParkAccountRecord> list = query.list();
			if(list!=null){
				for(ParkAccountRecord par : list){
					if(par!=null)
						data[par.getType()][par.getDay()-1] = Math.abs(par.getAmount());
				}
			}else{
				return null;
			}
			log.info( data.toString());
			return data;
		}
		return null;
	}

	/**
	 * 按年收支统计
	 * <p>Title: queryByMonth</p>
	 * <p>Description: 
	 * 		1. 统计本月的收入、支出  写入payInOut
	 * 		2. 统计本月按类别的费用金额，写入payStat
	 * 		3. 统计本月按天来计算的收入、支出map列表，写入paylistInOutByDay
	 * </p>
	 * @param payInOut  收入和支出列表
	 * @param payStat   根据category类型统计的列表
	 * @param paylistInOutByDay  按天排列的收入和支持列表
	 * @param daytime  日期，精确到年, e.g: 2014
	 * @param userId
	 * @return
	 * @throws Exception
	 * @see com.service.PayStatisticsService#queryByMonth(java.util.List, java.util.List, java.util.List, java.lang.String, java.lang.Long)
	 */
	public Double [][] queryByYear(List<ParkAccountRecord> payInOut,
			List<ParkAccountRecord> payStat,
			Map<Double, Double> paylistInOutByDay, String yeartime, Long userId)
					throws Exception {

		String ret = this.queryByTime(payInOut, payStat, yeartime, userId);//查询 payInOut、payStat
		if( ret.equals(GUtil.SUCCESS) ){
			//query paylistInOutByDay

			int enddate = 12;// outEndTime.getDate();
			Double[][] data = new Double[3][enddate];
			for(int i=0; i<3; i++){
				for(int j=0; j<enddate; j++){
					if(i==0)
						data[i][j] = (double)(j+1);
					else
						data[i][j] = 0.0;
				}
			}

			session = this.getSession();
			String hql = "SELECT new com.bean.ParkAccountRecord( year(createTime),month(createTime),sum(amount),type ) " +
					"from ParkAccountRecord "+
					"where parkManager.userId=? and createTime like '"+yeartime+"%'"+
					"GROUP BY year(createTime),month(createTime),type ";
			Query query = session.createQuery(hql);
			query.setLong(0, userId);
			List<ParkAccountRecord> list = query.list();
			if(list!=null){
				for(ParkAccountRecord par : list){
					if(par!=null)
						data[par.getType()][par.getMonth()-1] = Math.abs(par.getAmount());
				}
			}else{
				return null;
			}
			log.info(this.getClass()+".queryByYear()"+ data.toString());
			return data;
		}
		return null;
	}

}
